﻿namespace MXERP.Infrastructure.Store
{
    public partial class TabState
    {
        public static class Add
        {
            public record Action(string Url, string Title) : IAction;

            public class Handler : ActionHandler<Action>
            {
                public Handler(IStore store) : base(store) { }
                TabState TabState => Store.GetState<TabState>();

                public override Task Handle(Action action, CancellationToken cancellationToken)
                {
                    if (!TabState.Tabs.Any(x => x.Url == action.Url))
                    {
                        var tab = new Tab() { Url = action.Url, Title = action.Title ?? "", CreatedOn = DateTime.MinValue };
                        TabState.Tabs.Add(tab);
                    }
                    return Task.CompletedTask;
                }
            }
        }
    }
}
